class Rational {
    private int numerator;
    private int denominator;

    public Rational(int numerator, int denominator) {
        simplify(numerator, denominator);
    }

    public int getNumerator() {
        return numerator;
    }

    public int getDenominator() {
        return denominator;
    }

    public Rational add(Rational r) {
        int newNumerator = this.numerator * r.getDenominator() + this.denominator * r.getNumerator();
        int newDenominator = this.denominator * r.getDenominator();
        return new Rational(newNumerator, newDenominator);
    }

    public Rational sub(Rational r) {
        int newNumerator = this.numerator * r.getDenominator() - this.denominator * r.getNumerator();
        int newDenominator = this.denominator * r.getDenominator();
        return new Rational(newNumerator, newDenominator);
    }

    public Rational muti(Rational r) {
        int newNumerator = this.numerator * r.getNumerator();
        int newDenominator = this.denominator * r.getDenominator();
        return new Rational(newNumerator, newDenominator);
    }

    public Rational div(Rational r) {
        int newNumerator = this.numerator * r.getDenominator();
        int newDenominator = this.denominator * r.getNumerator();
        return new Rational(newNumerator, newDenominator);
    }

    private void simplify(int a, int b) {
        int gcd = findGCD(Math.abs(a), Math.abs(b));
        this.numerator = a / gcd;
        this.denominator = b / gcd;
    }

    private int findGCD(int a, int b) {
        if (a == 0) {
            return b;
        }
        while (b != 0) {
            if (a > b) {
                a = a - b;
            } else {
                b = b - a;
            }
        }
        return a;
    }
}